//
//  GuideViewController.m
//  ToWhere
//
//  Created by dllo on 15/11/25.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "GuideViewController.h"

@interface GuideViewController ()

@property(nonatomic, retain) UIScrollView *scrollView;

@property(nonatomic, retain) UIPageControl *page;

@end

@implementation GuideViewController

- (void)dealloc
{
    [_scrollView release];
    [_page release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createGuidePage];
}

-(void)createGuidePage
{
#pragma mark 创建滚动视图
    [self createScrollView];
    
}

-(void)createScrollView
{
    //创建scrollView
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * 4, 0);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [_scrollView release];
    
    NSArray *titleArray = @[@"First", @"Second", @"Third", @"Fouth"];
    for (int i = 0; i < 4; i++) {
        
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width * i, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
        imageview.image = [UIImage imageNamed:titleArray[i]];
        [self.scrollView addSubview:imageview];
        [imageview release];
        
        
        if (i == 3) {
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.backgroundColor = [UIColor colorWithRed:0.779 green:0.000 blue:0.000 alpha:1.000];
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 10;
            button.titleLabel.font = [UIFont systemFontOfSize:17 * kWidth];
            [button setTitle:@"立即体验" forState:UIControlStateNormal];
            button.frame = CGRectMake(self.scrollView.frame.size.width / 2 - 50 * kWidth + self.scrollView.frame.size.width * i, self.scrollView.frame.size.height - 125 * kHeight, kWidth * 100, kHeight * 40);
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.scrollView addSubview:button];
            [button addTarget:self action:@selector(enterAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        
    }
    
    //创建page
    self.page = [[UIPageControl alloc]initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 70 * kWidth, self.view.frame.size.height - 40 * kHeight, kWidth * 140, kHeight * 20)];
    self.page.numberOfPages = 4;
    [self.view addSubview:self.page];
    [self.page addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventTouchUpInside];
    [_page release];
}

-(void)enterAction:(UIButton *)btn
{
    [self.delegate enterHomepage];
}

//让scrollView和page建立连接
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.page.currentPage = scrollView.contentOffset.x / scrollView.frame.size.width ;
}
-(void)pageAction:(UIPageControl *)page
{
    self.scrollView.contentOffset = CGPointMake( self.scrollView.frame.size.width * page.currentPage, 0);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
